package com.itwk.gulimall.ware.controller;

import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.R;
import com.itwk.gulimall.ware.entity.WmsPurchaseEntity;
import com.itwk.gulimall.ware.service.WmsPurchaseService;
import com.itwk.gulimall.ware.vo.DonePurchaseVo;
import com.itwk.gulimall.ware.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 采购信息
 *
 * @author wangke
 * @email wangke19970512@163.com
 * @date 2021-01-08 11:54:18
 */
@RestController
@RequestMapping("ware/purchase")
@Transactional
public class WmsPurchaseController {
    @Autowired
    private WmsPurchaseService wmsPurchaseService;

    /*  /ware/purchase/unreceive/list
    * 合并采购项时查询未新建或者已分配的采购单，其他状态不可选
    * 状态 0-新建，1-已分配，2-正在采购，3-采购完成，采购失败；
    * */

    @GetMapping("/unreceive/list")
   public R getUnReceivePurchase(@RequestParam  Map<String, Object> params ){

     PageUtils page  = wmsPurchaseService.getUnReceivePurchase(params);

      return  R.ok().put("page", page);

   }

    /*
    * 合并采购项为采购单 /ware/purchase/merge
    *
    * */
    @PostMapping("/merge")
    public R mergePurchaseDetail(@RequestBody PurchaseVo purchaseVo){
        wmsPurchaseService.mergePurchaseDetail(purchaseVo); //合并采购项目
      return R.ok();
    }

    /*
    * 模拟采购系统接单
    * /ware/purchase/received
    * */
    @PostMapping("/received")
      public R receivedPurchase(@RequestBody List<Long> purchaseIds){
        wmsPurchaseService.receivedPurchase(purchaseIds);
        return R.ok();
      }

    /*
    * 完成采购
    *  /ware/purchase/done
    * */

    @PostMapping("/done")
    public R donePurchase(@RequestBody DonePurchaseVo donePurchaseVo){
        wmsPurchaseService.donePurchase(donePurchaseVo);
          return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
  //  @RequiresPermissions("ware:wmspurchase:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wmsPurchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("ware:wmspurchase:info")
    public R info(@PathVariable("id") Long id){
		WmsPurchaseEntity wmsPurchase = wmsPurchaseService.getById(id);

        return R.ok().put("wmsPurchase", wmsPurchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("ware:wmspurchase:save")
    public R save(@RequestBody WmsPurchaseEntity wmsPurchase){
        wmsPurchase.setCreateTime(new Date());
        wmsPurchase.setUpdateTime(new Date());
		wmsPurchaseService.save(wmsPurchase);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("ware:wmspurchase:update")
    public R update(@RequestBody WmsPurchaseEntity wmsPurchase){
		wmsPurchaseService.updateById(wmsPurchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("ware:wmspurchase:delete")
    public R delete(@RequestBody Long[] ids){
		wmsPurchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
